package com.wh.accesscontrol.controller;

import com.wh.accesscontrol.entity.Employee;
import com.wh.accesscontrol.entity.User;
import com.wh.accesscontrol.service.EmployeeService;
import com.wh.accesscontrol.service.UserService;
import com.wh.accesscontrol.util.MD5Util;
import com.wh.accesscontrol.util.RedisUtil;
import com.wh.accesscontrol.util.RetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
/**
 * 用户登录
 *
 * @author : WH
 * @date : 2020-02-27 17:02
 **/
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private EmployeeService employeeService;


    private Integer userid;
    private String password;


    /**
     * 登录
     *
     * @param userid   用户标识
     * @param password 密码
     * @return {@link Map<String, Object>}
     */
    @RequestMapping("/login")
    public Map<String, Object> login(Integer userid, String password, HttpServletRequest request) {
        User user = userService.findByUserId(userid);
        if (user == null) {
            return RetUtil.retError("用户不存在！");
        }

        if (password.equals(user.getPassword())) {
            //request.getSession().setAttribute("token",100);
            String token = MD5Util.md5(String.valueOf(new Date().getTime()) + user.getUserid());
            String bak=(String) redisUtil.get(user.getUserid()+"");
            if(bak!=null){
                redisUtil.del(bak);
            }
            redisUtil.set(user.getUserid()+"", token, 3600 * 24 * 7);
            redisUtil.set(token, user.getUserid(), 3600 * 24 * 7);
            HashMap<String, Object> map = new HashMap<>();
            map.put("access_token", token);
            map.put("role",user.getAuthority());


            return RetUtil.retSuccess(map);





        }
        return RetUtil.retError("密码错误！");
    }


@RequestMapping("/c")
    public Map<String,Object> c(){

        redisUtil.set("1",11111);
    System.out.println(redisUtil.get("1"));
        return RetUtil.retError();
//        String ID="4";
//        String b[]={"寝室","一栋","二栋","三栋","四栋","五栋"};
//        for (int i=1;i<5;i++){
//            for (int a=1;a<5;a++){
//        for (int j=1;j<40;j++){
//            String type="";
//            ID="4";
//            String n ="";
//            Employee employee = new Employee();
//            ID+=String.valueOf(i);
//            ID+=String.valueOf(a);
//            if(j<10){
//                ID+="0";
//                ID+=String.valueOf(j);
//                n=b[i]+a+0+j+"寝室";
//            }else {
//                ID+=String.valueOf(j);
//                n=b[i]+a+j+"寝室";
//
//        }
//            employee.setNumber(MathUtil.parseInt(ID));
//            type="4"+String.valueOf(i);
//            employee.setType( MathUtil.parseInt(type));
//            employee.setStadiumname(n);
//            employee.setLeader(b[i]+"寝室管理员");
//
//            System.out.println(employee.toString());
//
//            employeeService.add(employee);
//        }
//    }
//        }
//
//
//    return RetUtil.retSuccess();
}


    }
